<?php

namespace app\model\activity;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-LeadershipRecModel
 * Class LeadershipRecModel
 * @package app\model
 */
class LeadershipRecModel extends BaseModel
{
    use SoftDelete;
    protected $name = 'leadership_rec';


    /**
     * 范围查询-keyword
     *
     * @param [type] $query
     * @param [type] $keyword
     * @return void
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->where('a.title', 'like', "%{$keyword}%");
        }
    }

    /**
     * 范围查询-store_id
     *
     * @param [type] $query
     * @param [type] $store_id
     * @return void
     */
    public function scopeStore($query, $store_id)
    {
        if (!empty($store_id)) {
            $query->where('a.store_id', $store_id);
        }
    }

    /**
     * 范围查询-status
     *
     * @param [type] $query
     * @param [type] $status
     * @return void
     */
    public function scopeStatus($query, $status)
    {
        if (!empty($status)) {
            $query->where('a.status', $status);
        }
    }

    /**
     * 范围查询-is_hot
     *
     * @param [type] $query
     * @param [type] $is_hot
     * @return void
     */
    public function scopeHot($query, $is_hot)
    {
        if (!empty($is_hot)) {
            $query->order('read_count desc');
        }
    }

    /**
     * 范围查询-type
     *
     * @param [type] $query
     * @param [type] $type
     * @return void
     */
    public function scopeType($query, $type)
    {
        if (!empty($type)) {
            $query->where('a.type', $type);
        }
    }

    public function scopeIdArr($query, $idArr)
    {
        if (!empty($idArr)) {
            $query->where('a.id', 'in', $idArr);
        }
    }

    /**
     * 格式化
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function formatx(&$data)
    {
        $photo = $data['photo'];
        $data['photo_img'] = "";
        $photo_arr = [];
        if (!empty($photo)) {
            if (strpos($photo, ',') > 0) {
                $photo_arr = explode(',', $photo);
                $data['photo_img'] = $photo_arr[0];
            } else {
                $photo_arr[] = $photo;
                $data['photo_img'] = $photo;
            }
        }
        $data['photo'] = $photo_arr;
        if ((!empty($data['video_url'])) && empty($data['photo_img'])) {
            $data['photo_img'] = $data['video_url'] . "?x-oss-process=video/snapshot,t_5000,m_fast";
        }
        if (!empty($data['content'])) {
            $data['content'] = str_replace('width: 538px', 'width:100%', $data['content']);
            $data['content'] = str_replace('width: 532px', 'width:100%', $data['content']);
            $data['content'] = str_replace('width: 548px;', 'width:100%', $data['content']);
        }
        if (empty($data['goods_ids'])) {
            $data['goods_ids'] = [];
        } else {
            $data['goods_ids'] = json_decode($data['goods_ids'], true);
        }
    }

    /**
     * 获取分页
     * @param $map
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param $order
     * @param string $field
     * @return array
     */
    public function getPageData($map, $keyword, $current_page, $list_rows, $order = "sort desc,create_time desc", $field = 'a.id,a.create_time,a.title,a.desc,a.sort,a.tag,a.status,a.photo,a.video_url,a.type,a.read_count')
    {
        $dataList = $this
            ->alias('a')
            ->field($field)
            ->keyword($keyword)
            ->where($map)
            ->order($order)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }

    /**
     * 获取分页
     * @param $keyword
     * @param $status
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getDailyPageData($keyword, $status, $current_page, $list_rows, $field = 'a.id,a.create_time,a.title,a.desc,a.sort,a.tag,a.status,a.is_member,a.type,a.read_count')
    {
        $dataList = $this
            ->alias('a')
            ->field($field)
            ->keyword($keyword)
            ->status($status)
            ->where('type', 3)
            ->order('sort desc,create_time desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }

    /**
     * 获取分页
     * @param $keyword
     * @param $type
     * @param $status
     * @param $is_hot
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @param int $is_member
     * @param $idArr
     * @param string $order
     * @return array
     */
    public function getPageDataList($keyword, $type, $status, $is_hot, $current_page, $list_rows, $field = 'a.id,a.create_time,a.title,a.desc,a.sort,a.tag,a.status,a.type', $is_member = 1, $idArr, $order = "sort desc,create_time desc")
    {
        $dataList = $this
            ->alias('a')
            ->field($field)
            ->keyword($keyword)
            ->status($status)
            ->type($type)
            ->hot($is_hot)
            ->idArr($idArr)
            ->order($order)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }
}
